const router = require("koa-router")();

router.get("/", async (ctx, next) => {
    console.log("visit root");
    console.error("visit root");

    await ctx.render("index", {
        title: "Hello Koa 2!",
    });
});

router.get("/json", async (ctx, next) => {
    // 响应体 对象变json响应给客户端
    ctx.body = {
        title: "koa2 json",
    };
});

router.get("/session-test", async function (ctx, next) {
    if (ctx.session.viewCount == null) {
        ctx.session.viewCount = 0;
    }
    ctx.session.viewCount++;

    ctx.cookies.set("viewCount", ctx.session.viewCount);
    ctx.body = {
        errno: 0,
        viewCount: ctx.session.viewCount,
    };
});

module.exports = router;
